/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SMART_SERVE_PLUGIN_LLAMACPP_CREATOR_H
#define SMART_SERVE_PLUGIN_LLAMACPP_CREATOR_H

#include <cstdlib>
#include <filesystem>

#include <assertion.h>
#include <smart_serve_log.h>

#include <model/creator.h>
#include <session/creator.h>

namespace OHOS {
namespace llamacpp {

#define TAG "[llamacpp | Creator] "

template<typename ModelAdapter>
class ModelCreator : public SmartServe::ModelCreator {
public:
    ModelCreator(const std::string& name, const std::string& filePath) : name_(name), filePath_(filePath)
    {}

    const std::string& Name() const override
    {
        return name_;
    }

    std::unique_ptr<SmartServe::ModelAdapter> Create() override
    {
        if (!std::filesystem::exists(filePath_)) {
            SMART_SERVE_LOGE(TAG "%s : model file %s not found", name_.c_str(), filePath_.c_str());
            return nullptr;
        }

        return std::make_unique<ModelAdapter>(name_, filePath_);
    }

private:
    std::string name_;
    std::string filePath_;
};

template<typename SessionAdapter>
class SessionCreator : public SmartServe::SessionCreator {
public:
    SessionCreator(const std::string& name, const std::vector<std::string>& depModels)
        : name_(name), depModels_(depModels)
    {}

    const std::string& Name() const override
    {
        return name_;
    }

    const std::vector<std::string>& DepModels() const override
    {
        return depModels_;
    }

    std::unique_ptr<SmartServe::SessionAdapter> Create(const std::vector<SmartServe::ModelAdapter*>& modelAdapters) override
    {
        ASSERT(modelAdapters.size() == depModels_.size());
        return std::make_unique<SessionAdapter>(name_, modelAdapters);
    }

    std::string name_;
    std::vector<std::string> depModels_;
};

#undef TAG

} // namespace llamacpp
} // namespace OHOS

#endif // SMART_SERVE_PLUGIN_LLAMACPP_CREATOR_H
